package com.platform.mall.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.List;

/**
 * Kni4j配置类
 *
 * @author haoxr
 * @date 2021-02-25 15:36
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfiguration {


    @Bean
    public Docket restAdminApi(Environment environment) {

        //设置只在开发中环境中启动swagger
        Profiles profiles = Profiles.of("dev");
        //表示如果现在是dev环境，则返回true 开启swagger
        boolean flag = environment.acceptsProfiles(profiles);

        /*添加接口请求头参数配置 没有的话 可以忽略*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").defaultValue("设置token默认值")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).groupName("管理后天接口")
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.platform.mall.controller.admin"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }

    @Bean
    public Docket restAppApi(Environment environment) {

        //设置只在开发中环境中启动swagger
        Profiles profiles = Profiles.of("dev");
        //表示如果现在是dev环境，则返回true 开启swagger
        boolean flag = environment.acceptsProfiles(profiles);

        /*添加接口请求头参数配置 没有的话 可以忽略*/
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").defaultValue("设置token默认值")
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2).groupName("app接口")
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.platform.mall.controller.app"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("系统管理")
                .description("<div style='font-size:14px;color:red;'>用户、角色、部门、菜单、权限、字典、客户端接口</div>")
                .termsOfServiceUrl("https://www.youlai.tech")
                .contact(new Contact("有来技术团队", "https://gitee.com/youlaiorg", "youlaitech@163.com"))
                .license("Open Source")
                .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
                .version("1.0.0")
                .build();
    }

}
